//------------Sonic CD Boss Face Script-------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

//-------Aliases-------//
#alias 46: TYPE_BOSSFACE
#alias object[currentPlayer].Value4:object[currentPlayer].Invincible

// Function declarations
#function BossFace_Function49

function BossFace_Function49
	switch Object.Value0
	case 0
		temp0=1
		break
	case 1
		temp0=0
		break
	case 2
		temp0=2
		break
	case 3
		temp0=0
		break
	case 4
		temp0=3
		break
	case 5
		temp0=0
		break
	case 6
		temp0=4
		break
	case 7
		temp0=5
		break
	case 8
		temp0=5
		break
	case 9
		temp0=6
		break
	case 10
		temp0=6
		break
	case 11
		temp0=10
		break
	case 12
		temp0=4
		break
	case 13
		temp0=10
		break
	case 14
		temp0=8
		break
	case 15
		temp0=50
		break
	case 16
		temp0=5
		break
	case 17
		temp0=10
		break
	case 18
	case 19
		temp0=-1
		break

	endswitch
endfunction


event ObjectMain
	switch Object.State
	case 0
		Object.Animation=-1
		Object.Priority=1
		Object.Value1=Object.entityPos
		Object.Value1+=1
		Object.Value2=Object.entityPos
		Object.Value2+=8
		Object.Value5=Object[+1].iXPos
		Object.Value6=Object.Value5
		Object.Value6+=320
		temp0=screen.xCenter
		temp0-=160
		temp0>>=1
		Object.Value6+=temp0
		Object.Value7=Stage.curXBoundary2
		Object.Scale=Stage.curYBoundary2
		Stage.newXBoundary2=Object.Value6
		Object.State=1
		break
	case 1
		temp0=Object.Value5
		if screen.xCenter>160
			temp0+=180
		else
			temp0+=160
		endif
		temp0<<=16
		if object[currentPlayer].XPos>temp0
			temp0=Object.YPos
			temp0+=12582912
			if object[currentPlayer].YPos>temp0
				Stage.newXBoundary1=Object.Value5
				Stage.curYBoundary2=object[currentPlayer].YPos
				Stage.curYBoundary2>>=16
				Stage.curYBoundary2+=160
				if Stage.curYBoundary2<256
					Stage.curYBoundary2=256
				endif
				Object.State=6
			endif
		endif
		break
	case 2
		if Object.Rotation>0
			Object.Rotation--
		endif
		if Object[+1].State>2
			if Object.Direction>FACING_RIGHT
				Object.Direction--
			else
				Object.Animation=0
			endif
		endif
		ArrayPos0=Object.Value1
		GetBit(temp0,Object[ArrayPos0].Value4,0)
		if temp0==1
			if Object[ArrayPos0].State==6
				Object.YPos=Object[+15].Value0
				Object[+1].YPos=Object[+15].Value1
				Object[+2].YPos=Object[+15].Value2
				Object[+3].Value7=Object[+15].Value3
				Object[+3].YPos=Object[+15].YPos
				Object[+4].Value7=Object[+15].Value4
				Object[+5].YPos=Object[+15].Value5
				Object[+6].Value7=Object[+15].Value6
				Object[+7].Value7=Object[+15].Value7
				GetBit(temp0,Object[ArrayPos0].Value4,6)
				if temp0==0
					SetBit(Object[ArrayPos0].Value4,6,1)
				else
					SetBit(Object[ArrayPos0].Value4,6,0)
				endif
			endif
			SetBit(Object[ArrayPos0].Value4,0,0)
			if Object.Value0==2
				SetBit(Object.Value4,3,1)
			endif
			SetBit(Object[ArrayPos0].Value4,1,0)
			Object.Value0+=2
			CallFunction(BossFace_Function49)
			if temp0<0
				Object.Value0=6
			endif
			CallFunction(BossFace_Function49)
			if temp0==6
				Object[+15].Value0=Object.YPos
				Object[+15].Value1=Object[+1].YPos
				Object[+15].Value2=Object[+2].YPos
				Object[+15].Value3=Object[+3].Value7
				Object[+15].YPos=Object[+3].YPos
				Object[+15].Value4=Object[+4].Value7
				Object[+15].Value5=Object[+5].YPos
				Object[+15].Value6=Object[+6].Value7
				Object[+15].Value7=Object[+7].Value7
			endif
			if Object[ArrayPos0].State==5
				if temp0==4
					GetBit(temp1,Object[ArrayPos0].Value4,6)
					if temp1==0
						SetBit(Object[ArrayPos0].Value4,6,1)
					else
						SetBit(Object[ArrayPos0].Value4,6,0)
					endif
				endif
			endif
			Object[ArrayPos0].State=temp0
			Object.Value0++
			CallFunction(BossFace_Function49)
			Object.Value0--
			Object[ArrayPos0].Value5=temp0
		endif
		break
	case 3
		if Object.Rotation<160
			Object.Rotation++
			temp0=Object.Rotation
			temp0&=3
			if temp0==3
				Rand(temp0,128)
				temp0-=64
				temp0<<=16
				temp0+=Object.XPos
				Rand(temp1,64)
				temp1-=32
				temp1<<=16
				temp1+=Object.YPos
				CreateTempObject(TypeName[Explosion],0,temp0,temp1)
				PlaySfx(22,0)
			endif
			if Object.Rotation==94
				temp0=Object.entityPos
				temp0++
				player.score+=1000
				ResetObjectEntity(temp0,TypeName[EggmanJetpack],0,Object.XPos,Object.YPos)
				Object.Animation=3
#platform: Use_Haptics
				HapticEffect(76,0,0,0)
#endplatform
			endif
		endif
		break
	case 4
		break
	case 5
		if Object.Rotation<120
			Music.Volume--
			Stage.curYBoundary2+=4
			Stage.curXBoundary2+=6
			Object.Rotation++
		else
			Stage.curYBoundary2=Object.Scale
			Stage.curXBoundary2=Object.Value7
			PlayMusic(0)
			Object.Type=TypeName[BlankObject]
		endif
		break
	case 6
		if Stage.curYBoundary2>Object[+1].iYPos
			Stage.curYBoundary2-=2
			Stage.curYBoundary1+=2
		else
			Stage.curYBoundary2=Object[+1].iYPos
			temp0=Object[+1].iYPos
			temp0-=Screen.YSize
			if temp0<0
				temp0=0
			endif
			Stage.newYBoundary1=temp0
			Object.Rotation=0
			Object.State=7
		endif
		break
	case 7
		if Object.Rotation<120
			if Object.Rotation<100
				Music.Volume--
			else
				if Object.Rotation==100
					PlayMusic(4)
				endif
			endif
			Object.Rotation++
		else
			Object.Rotation=0
			Object.State=2
			Object.Animation=1
			ArrayPos0=Object.entityPos
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossBody],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=2
			Object[ArrayPos0].Value2=Object.entityPos
			Object[ArrayPos0].Value2+=5
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossLegJoint],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=1
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=3
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossLeg],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=2
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=4
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossFoot],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=3
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=1
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossLegJoint],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].DrawOrder=2
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=1
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=6
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossLeg],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].DrawOrder=2
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=5
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=7
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossFoot],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].DrawOrder=2
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=6
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=1
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossShoulder],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].DrawOrder=4
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=9
			Object[ArrayPos0].Value2=Object.entityPos
			Object[ArrayPos0].Value2+=12
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossArmJoint],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=8
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=10
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossArm],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=9
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=11
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossBumper],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=10
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=1
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossArmJoint],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].DrawOrder=2
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=8
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=13
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossArm],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].DrawOrder=2
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=12
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=14
			ArrayPos0++
			ResetObjectEntity(ArrayPos0,TypeName[BossBumper],0,Object.XPos,Object.YPos)
			Object[ArrayPos0].Priority=1
			Object[ArrayPos0].DrawOrder=2
			Object[ArrayPos0].Value0=Object.entityPos
			Object[ArrayPos0].Value0+=13
			Object[ArrayPos0].Value1=Object.entityPos
			Object[ArrayPos0].Value1+=1
			ArrayPos0++
		endif
		break

	endswitch

foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
	temp0=object[currentPlayer].CollisionRight
	temp0<<=16
	temp0+=object[currentPlayer].XPos
	temp1=Stage.curXBoundary2
	temp1<<=16
	if temp0>temp1
		object[currentPlayer].XVel=0
		object[currentPlayer].Speed=0
		object[currentPlayer].XPos=temp1
		temp0=object[currentPlayer].CollisionRight
		temp0<<=16
		object[currentPlayer].XPos-=temp0
	endif
	if Object.State>1
		if object[currentPlayer].Animation!=ANI_HURT
			if Object.Value3<3
				BoxCollisionTest(C_TOUCH, object.entityPos,-28,-28,28,28, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if CheckResult==1
					if object[currentPlayer].Animation==ANI_JUMPING
						object[currentPlayer].XVel=object[currentPlayer].XPos
						object[currentPlayer].XVel-=Object.XPos
						object[currentPlayer].XVel>>=3
						object[currentPlayer].Speed=object[currentPlayer].XVel
						object[currentPlayer].YVel=object[currentPlayer].YPos
						object[currentPlayer].YVel-=Object.YPos
						object[currentPlayer].YVel>>=3
						if Object.Rotation==0
							if Object[+1].State!=1
								PlaySfx(22,0)
#platform: Use_Haptics
								HapticEffect(75,0,0,0)
#endplatform
								switch Object.Value3
								case 0
									Object.Value3++
									Object.Rotation=30
									Object.Animation=2
									Object.Direction=120
									ArrayPos0=Object.Value2
									ArrayPos1=Object[ArrayPos0].Value2
									Object[ArrayPos1].State=5
									Object[ArrayPos1].Value2=65536
									Object[ArrayPos1].Value5=131072
									ArrayPos0=Object[ArrayPos1].Value1
									Object[ArrayPos0].State=2
									Object[ArrayPos0].Value2=0
									Object[ArrayPos0].Value5=65536
									ArrayPos1=Object[ArrayPos0].Value1
									Object[ArrayPos1].State=2
									Object[ArrayPos1].Value2=0
									Object[ArrayPos1].Value5=-98304
									break
								case 1
									Object.Value3++
									Object.Rotation=30
									Object.Animation=2
									Object.Direction=120
									ArrayPos0=Object.Value2
									ArrayPos1=Object[ArrayPos0].Value1
									Object[ArrayPos1].State=5
									Object[ArrayPos1].Value2=65536
									Object[ArrayPos1].Value5=131072
									ArrayPos0=Object[ArrayPos1].Value1
									Object[ArrayPos0].State=2
									Object[ArrayPos0].Value2=0
									Object[ArrayPos0].Value5=65536
									ArrayPos1=Object[ArrayPos0].Value1
									Object[ArrayPos1].State=2
									Object[ArrayPos1].Value2=0
									Object[ArrayPos1].Value5=-98304
									break
								case 2
									Object.Value3++
									Object.State=3
									Object.Animation=2
									ArrayPos0=Object.Value1
									Object[ArrayPos0].State=7
									ArrayPos0=Object.Value2
									Object[ArrayPos0].State=2
									Object[ArrayPos0].Value2=-65536
									Object[ArrayPos0].Value5=-131072
									ArrayPos0=Object.Value1
									ArrayPos1=Object[ArrayPos0].Value1
									Object[ArrayPos1].State=12
									Object[ArrayPos1].Value2=0
									Object[ArrayPos1].Value5=-163840
									ArrayPos0=Object[ArrayPos1].Value1
									Object[ArrayPos0].State=7
									Object[ArrayPos0].Value2=0
									Object[ArrayPos0].Value5=-131072
									ArrayPos1=Object[ArrayPos0].Value1
									Object[ArrayPos1].State=6
									Object[ArrayPos1].Value2=0
									Object[ArrayPos1].Value5=-143360
									ArrayPos0=Object.Value1
									ArrayPos1=Object[ArrayPos0].Value2
									Object[ArrayPos1].State=12
									Object[ArrayPos1].Value2=0
									Object[ArrayPos1].Value5=-163840
									ArrayPos0=Object[ArrayPos1].Value1
									Object[ArrayPos0].State=7
									Object[ArrayPos0].Value2=0
									Object[ArrayPos0].Value5=-131072
									ArrayPos1=Object[ArrayPos0].Value1
									Object[ArrayPos1].State=6
									Object[ArrayPos1].Value2=0
									Object[ArrayPos1].Value5=-143360
									break

								endswitch
							endif
						endif
					else
						if object[currentPlayer].state != PlayerObject_Death
							if object[currentPlayer].value7 == 0
								if object[currentPlayer].value8 == 0
									object[currentPlayer].state = PlayerObject_Hurt
									if object[currentPlayer].xpos > object.xpos
										object[currentPlayer].speed = 0x20000
									else
										object[currentPlayer].speed = -0x20000
									end if
								end if
							end if
						end if
						checkResult = 0
					endif
				endif
			endif
		endif
	endif
next
end event


event ObjectDraw
	switch Object.Animation
	case 0
		DrawSprite(0)
		break
	case 1
		if Object.InkEffect<8
			Object.InkEffect=8
		else
			if Object.InkEffect>23
				Object.InkEffect=8
			endif
		endif
		Object.Frame=Object.InkEffect
		Object.Frame>>=3
		Object.InkEffect++
		DrawSprite(Object.Frame)
		break
	case 2
		if Object.InkEffect<12
			Object.InkEffect=12
		else
			if Object.InkEffect>27
				Object.InkEffect=12
			endif
		endif
		Object.Frame=Object.InkEffect
		Object.Frame>>=2
		Object.InkEffect++
		DrawSprite(Object.Frame)
		break

	endswitch
endevent


event ObjectStartup
	CheckCurrentStageFolder("RRZ")
	if checkResult == 1
		LoadSpriteSheet("RRZ/Objects.gif")
		SpriteFrame(-28,-24,40,16,115,34)
		SpriteFrame(-28,-24,40,16,115,51)
		SpriteFrame(-28,-24,40,16,115,68)
		SpriteFrame(-28,-40,40,32,66,1)
		SpriteFrame(-28,-24,40,16,115,85)
		SpriteFrame(-28,-40,40,32,107,1)
		SpriteFrame(-28,-24,40,16,115,102)
	else
		LoadSpriteSheet("R1/Objects3.gif")
		SpriteFrame(-28,-24,40,16,206,1)
		SpriteFrame(-28,-24,40,16,206,18)
		SpriteFrame(-28,-24,40,16,206,35)
		SpriteFrame(-28,-40,40,32,124,1)
		SpriteFrame(-28,-24,40,16,206,52)
		SpriteFrame(-28,-40,40,32,165,1)
		SpriteFrame(-28,-24,40,16,206,69)
	endif
endevent

event RSDK
	LoadSpriteSheet("Global/Display.gif")
	SetEditorIcon(Icon0,SingleIcon,-16,-16,32,32,1,143)
endevent
